package com.pf.service.impl;

import com.pf.dto.AddAddressReq;
import com.pf.mapper.AddressMapper;
import com.pf.mapper.entity.Address;
import com.pf.service.AddressService;
import com.pf.utils.AESUtils;
import com.pf.utils.DTOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addAddress(AddAddressReq req) {
        Address address = DTOUtils.copyToNewObj(req, Address.class);
        address.setAddressNameCipher(AESUtils.encrypt(req.getAddressName(), AESUtils.AES_KEY));
        return addressMapper.insertAddress(address);
    }

    @Override
    public List<Address> getAllAddress() {
        return addressMapper.getAllAddress();
    }

    @Override
    public Address getAddress(String addressName) {
        return addressMapper.getAddress(addressName);
    }
}
